<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of mmw_command_CommandResolver
 *
 * @author Manu
 */
require_once("mmw_command_Command.php");
require_once("mmw_command_ReadBookmarksByTagCommand.php");
require_once("mmw_command_ReadBookmarksByUrlCommand.php");
require_once("mmw_command_CreateBookmarkCommand.php");
require_once("mmw_command_DeleteBookmarkCommand.php");
require_once("mmw_command_ReadTagsByTagCommand.php");
require_once(dirname(__FILE__) . "/../core/mmw_core_AppException.php");
require_once(dirname(__FILE__) . "/../controller/mmw_controller_Request.php");

class mmw_command_CommandResolver {

    public function getCommand(mmw_controller_Request $request) {
        if ($request->getProperty("target") == NULL) {
            throw new mmw_core_AppException("Missing parameter 'target'");
        } else if ($request->getProperty("action") == NULL) {
            throw new mmw_core_AppException("Missing parameter 'action'");
        }

        $command = NULL;
        $isSuccess = true;
        $errorMsg = "";

        $target = $request->getProperty("target");
        $action = $request->getProperty("action");

        switch ($target) {
            case "bookmark":
                switch ($action) {
                    case "readbytag":
                        $command = new mmw_command_ReadBookmarksByTagCommand();
                        break;
                    case "readbyurl":
                        $command = new mmw_command_ReadBookmarksByUrlCommand();
                        break;
                    case "create":
                        $command = new mmw_command_CreateBookmarkCommand();
                        break;
                    case "delete":
                        $command = new mmw_command_DeleteBookmarkCommand();
                        break;
                    default:
                        $isSuccess = false;
                        $errorMsg = "Unknown value '$action' for parameter 'action'";
                }
                break;
            case "tag":
                switch ($action) {
                    case "readbytag":
                        $command = new mmw_command_ReadTagsByTagCommand();
                        break;
                }
                break;
            default:
                $isSuccess = false;
                $errorMsg = "Unknown value '$target' for parameter 'target'";
        }

        if (!$isSuccess) {
            throw new mmw_core_AppException($errorMsg);
        }

        return $command;
    }

}

//*******tests
//$request=new mmw_controller_Request();
//$commandResolver=new mmw_command_CommandResolver();
//$test=$commandResolver->getCommand($request);
?>
